<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="dk"> 
<head> 
<link rel="shortcut icon" href="http://sofa-it.dk/wp-content/themes/sofa/images/favicon.ico"> 
<meta http-equiv="content-Language" content="en" /> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style/style.css"/>  
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<title>SOFA - SOcial og FAlig</title> 
</head> 
<body>

<div style="width: 500px; position: absolute; left: 50%; margin-left: -250px;">
<div style="width:100%;text-align: center;font-family: Centery Gothic, Verdana;">
	<span style="font-size: 50px; font-weight: bold;">INSTALL:</span><br/>
	<span style="font-size:25px;">SOFA TILMELDING</span>
</div>

<?php


if(isset($_POST['user']) && isset($_POST['dbname'])){
	echo '<div id="installSQL"><p>';
	installSQL($_POST['user'],$_POST['pass'],$_POST['serv'],$_POST['dbname']);
	echo '</p></div>';
}else{
	echo '<div id="installSQL">' .
			'<form method="POST" action="install.php">' .
			'Username:<br/> <input type="text" name="user" /><br/>' .
			'Password:<br/> <input type="password" name="pass" /><br/>' .
			'Host-adr:<br/> <input type="text" name="serv" /><br/>' .
			'DB name :<br/> <input type="text" name="dbname" /><br/>' .
			'<input id="submit" type="submit" value="Install" />' .
			'</form>' .
		'</div>';
}

function installSQL($user = "root",$pass,$serv = "localhost",$dbname){
	echo "<<< CONNECTION TO " . $serv . "<br/><<< WITH USER: " . $user . "!<br/>";
	
	$c = 0;
	
	$conn = mysql_connect($serv,$user,$pass) or die("<<< WRONG SERVER ADR., PASSWORD OR USERNAME!");
	
	$sql = sprintf("CREATE DATABASE IF NOT EXISTS %s", $dbname);
	
	if(mysql_query($sql,$conn)){
		echo "<<< CREATING DB: " . $dbname . "<br>";
		mysql_select_db($dbname,$conn);
		$c++;
	}else{
		echo "<b>ERROR CREATING DATABASE: </b> " . $dbname . "</br><<< " . mysql_error();
		exit;
	}
	
	$sql = "CREATE TABLE IF NOT EXISTS events(" .
			"ID MEDIUMINT NOT NULL AUTO_INCREMENT," .
			"name MEDIUMTEXT NOT NULL," .
			"info LONGTEXT NOT NULL," .
			"max INT(4) NOT NULL," .
			"enddate INT(12) NOT NULL," .
			"PRIMARY KEY(ID)" .
			")ENGINE InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci";
	
	if(mysql_query($sql,$conn)){
		echo "<<< CREATING EVENTS TABLE!<br>";
		$c++;
	}else{
		echo "<<< <b>ERROR CREATING EVENTS TABLE!</b></br><<< " . mysql_error();
	}
	
	$sql = "CREATE TABLE IF NOT EXISTS members(" .
			"SN INT(8) NOT NULL," .
			"name MEDIUMTEXT NOT NULL," .
			"date INT(12) NOT NULL," .
			"PRIMARY KEY(SN)" .
			")";
	if(mysql_query($sql,$conn)){
		echo "<<< CREATING MEMBERS TABLE..<br>";
		$c++;
	}else{
		echo "<<< <b>ERROR CREATING MEMBERS TABLE: </b></br><<< " . mysql_error();
	}
	
	$sql = "CREATE TABLE IF NOT EXISTS pendingmembers(" .
			"SN INT(8) NOT NULL," .
			"name MEDIUMTEXT NOT NULL," .
			"PRIMARY KEY(SN)" .
			")";
	if(mysql_query($sql,$conn)){
		echo "<<< CREATING PENDINGMEMBERS TABLE..<br>";
		$c++;
	}else{
		echo "<<< <b>ERROR CREATING PENDINGMEMBERS TABLE: </b></br><<< " . mysql_error();
	}	
	
	$sql = "CREATE TABLE IF NOT EXISTS users(" .
			"user VARCHAR(90) NOT NULL," .
			"pass VARCHAR(20) NOT NULL," .
			"mail MEDIUMTEXT NOT NULL," .
			"PRIMARY KEY(user)" .
			")";
	if(mysql_query($sql,$conn)){
		echo "<<< CREATING USERS TABLE..<br>";
		$c++;
		$sql = "INSET INTO users(user,pass,mail)" .
				"VALUES('admin','password','my@mail.com');";
		if(mysql_query($sql,$conn)){
			echo "<<< ADDED ADMIN:PASSWORD to users</br>";
			$c++;
		}else{
			echo "<<< ERROR ADDED ADMIN</br>";
		}
	}else{
		echo "<<< <b>ERROR CREATING USERS TABLE: </b></br><<< " . mysql_error();
	}	
	
	if($c == 5){
		echo "<<< DB's CREATED SUCCESFULLY!! <br>";	
	}else{
		echo "<<< DB's CREATION FAILED <br/><<< " . $c . " of 4 failed...";
	}	
	
	
	mysql_close($conn);
	
	echo "<<< CONNECTION CLOSED!";
}


?>
</div></body></html>